package hamsterpolice.todo.data;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Base64;

public class Encryption {
	
	private static final byte[] SALT = "ijfh98nhoiuwfhao3fpjfihehfzo83h21".getBytes();
	private static final int HASH_ITERATIONS = 1000;
	private static MessageDigest md = null;
	
	/**
	 * Hashes a value with a predetermined salt value
	 * @param value
	 * @return
	 */
	public static String hash(String value){
		if(md==null){
			try {
				md = MessageDigest.getInstance("SHA-512");
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				throw new RuntimeException("SHA-512 Encryption is not supported");
			}
		}
		
		byte[] bVal = value.getBytes();

		md.update(bVal);
		byte[] digest = md.digest(SALT);
		
		for(int i=0; i<HASH_ITERATIONS; i++){
			md.update(digest);
			digest = md.digest(SALT);
		}
		
		return Base64.encodeToString(digest, Base64.DEFAULT);
	}
}
